﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AAA.Forms.Components
{
    public partial class PictureBoxX : PictureBox
    {
        private Image _imgMaskImage;
        private Image _imgSelectedImage;
        private Image _imgNormalBackgroundImage;
        private Image _imgMouseOverImage;
        private Image _imgMouseDownImage;
        private string _strSelectedImage;
        private string _strNormalBackgroundImage;
        private string _strMouseOverImage;
        private string _strMouseDownImage;
        private int _iOffsetX;
        private bool _isCutBoundary;
        private bool _isSelected;
        private bool _isCatchMouseEvent = true;

        public bool IsCatchMouseEvent
        {
            get { return _isCatchMouseEvent; }
            set { _isCatchMouseEvent = value; }
        }

        public Image SelectedImage
        {
            get { return _imgSelectedImage; }
            set { _imgSelectedImage = value; }
        }

        public Image MaskImage
        {
            get { return _imgMaskImage; }
            set { _imgMaskImage = value; }
        }

        public bool IsCutBoundary
        {
            get { return _isCutBoundary; }
            set { _isCutBoundary = value; }
        }

        public Image NormalBackgroundImage
        {
            get { return _imgNormalBackgroundImage; }
            set { _imgNormalBackgroundImage = value; }
        }

        public Image MouseOverImage
        {
            get { return _imgMouseOverImage; }
            set { _imgMouseOverImage = value; }
        }

        public Image MouseDownImage
        {
            get { return _imgMouseDownImage; }
            set { _imgMouseDownImage = value; }
        }

        public int OffsetX
        {
            get { return _iOffsetX; }
            set { _iOffsetX = value; }
        }

        public PictureBoxX()
        {
            MouseEnter += new EventHandler(MouseEnterEvent);
            MouseMove += new MouseEventHandler(MouseMoveEvent);
            MouseDown += new MouseEventHandler(MouseDownEvent);
            MouseLeave += new EventHandler(MouseLeaveEvent);

            BackColor = Color.Transparent;
            SizeMode = PictureBoxSizeMode.AutoSize;
            BorderStyle = BorderStyle.None;
            OffsetX = 0;
            DoubleBuffered = true;
            IsCutBoundary = true;

            ResizeRedraw = true;
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //FlatAppearance.BorderSize = 0;
            //border = BorderStyle.None;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //if ((Visible == false) || (Text == ""))
            if(Text == "")
                return;
            PaintTitle(e.Graphics);
        }

        protected void PaintTitle(Graphics g)
        {
            SizeF fontSize;
            int iStartX;
            int iStartY;

            fontSize = g.MeasureString(Text, Font);

            iStartX = (int)((Width - fontSize.Width) / 2); ;
            if(OffsetX != 0)
                iStartX = OffsetX;

            iStartY = (int)((Height - fontSize.Height) / 2);

            g.DrawString(Text, Font, new SolidBrush(ForeColor),
                         new Point(iStartX, iStartY));
        }


        void MouseLeaveEvent(object sender, EventArgs e)
        {
            if (IsCatchMouseEvent == false)
                return;

            if ((NormalBackgroundImage == null) && (_strNormalBackgroundImage != null))
                NormalBackgroundImage = Image.FromFile(_strNormalBackgroundImage);
            if ((SelectedImage == null) && (_strSelectedImage != null))
                SelectedImage = Image.FromFile(_strSelectedImage);
            ChangeImage(_isSelected ? SelectedImage : NormalBackgroundImage);
        }

        void MouseDownEvent(object sender, MouseEventArgs e)
        {
            if (IsCatchMouseEvent == false)
                return;
            if ((MouseDownImage == null) && (_strMouseDownImage != null))
                MouseDownImage = Image.FromFile(_strMouseDownImage);
            ChangeImage(MouseDownImage);
        }

        void MouseMoveEvent(object sender, MouseEventArgs e)
        {
            if (IsCatchMouseEvent == false)
                return;
            if ((MouseOverImage == null) && (_strMouseOverImage != null))
                MouseOverImage = Image.FromFile(_strMouseOverImage);
            ChangeImage(MouseOverImage);
        }

        void MouseEnterEvent(object sender, EventArgs e)
        {
            if (IsCatchMouseEvent == false)
                return;
            if ((MouseOverImage == null) && (_strMouseOverImage != null))
                MouseOverImage = Image.FromFile(_strMouseOverImage);
            ChangeImage(MouseOverImage);
        }

        public void LoadImage(string strNormal, string strMouseOver, string strMouseDown)
        {
            LoadImage(strNormal, strMouseOver, strMouseDown, strNormal);
        }
        
        public void LoadImage(string strNormal, string strMouseOver, string strMouseDown, string strSelected)
        {
            try
            {
                NormalBackgroundImage = Image.FromFile(strNormal);
                _strNormalBackgroundImage = strNormal;
                if (IsCutBoundary)
                {
                    if (MaskImage == null)
                        Util.BitmapRegion.CreateControlRegion(this, NormalBackgroundImage);
                    else
                        Util.BitmapRegion.CreateControlRegion(this, MaskImage);
                }
                ChangeImage(NormalBackgroundImage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            try
            {
                MouseOverImage = Image.FromFile(strMouseOver);
                _strMouseOverImage = strMouseOver;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            try
            {
                MouseDownImage = Image.FromFile(strMouseDown);
                _strMouseDownImage = strMouseDown;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            try
            {
                SelectedImage = Image.FromFile(strSelected);
                _strSelectedImage = strSelected;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
        }

        public void SetUnselected()
        {
            _isSelected = false;
            if ((NormalBackgroundImage == null) && (_strNormalBackgroundImage != null))
                NormalBackgroundImage = Image.FromFile(_strNormalBackgroundImage);
            ChangeImage(NormalBackgroundImage);
        }

        public void SetSelected()
        {
            try
            {
                _isSelected = true;
                if ((SelectedImage == null) && (_strSelectedImage != null))
                    SelectedImage = Image.FromFile(_strSelectedImage);
                ChangeImage(SelectedImage);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void ChangeImage(Image image)
        {
            try
            {
                if (image != null)
                    Image = image;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }
    }
}
